# 自定义的断言类
from Lib.base.keywords import KeyWords
from Lib.common.ui_log import error_log, normal_log
import time


class AssertClass:

    def __init__(self, driver):
        self.kw = KeyWords(driver)

    def make_assert_by_text(self, args: str, assert_data: str, context=None):
        '''
        根据文本信息设置断言
        :param args: 断言的元素
        :param assert_data: 断言的值
        :param context:
        :return:
        '''
        try:
            text = self.kw.get_text(args=args, context=None)
            assert assert_data in text, '断言失败，没有获得对应的文本信息'
        except Exception as e:
            # 判断是否暂无数据,是，用例执行通过
            try:
                args = ('xpath', '//p[contains(text(), "暂无数据")]')
                self.kw.locator(args=args, context="暂无数据")
            except Exception as E:
                error_log().debug(e)
                raise e

    def make_assert_by_compare_time(self, args1: str, args2: str, context=None):
        '''
        按时间进行排序后 比较两个时间判断排序是否正确
        :param args1: 断言的时间元素1
        :param args2: 断言的时间元素2
        :param context:
        :return:
        '''
        try:
            time1_str1 = self.kw.get_text(args=args1, context=None)
            # 将第一个时间转化为时间戳
            # 判断时间是否包含小时
            if ':' in time1_str1:
                # 判断时间是否包含秒
                if len(time1_str1.split(':')) == 3:
                    time1 = time.mktime(time.strptime(time1_str1.strip(), '%Y-%m-%d %H:%M:%S'))
                else:
                    time1 = time.mktime(time.strptime(time1_str1.strip(), '%Y-%m-%d %H:%M'))
            else:
                time1 = time.mktime(time.strptime(time1_str1.strip(), '%Y-%m-%d'))
            # 判断第二时间是否存在
            if self.kw.element_is_exist(args=args2, context="第二条数据"):
                time1_str2 = self.kw.get_text(args=args2, context=None)
                if ':' in time1_str2:
                    if len(time1_str2.split(':')) == 3:
                        time2 = time.mktime(time.strptime(time1_str2.strip(), '%Y-%m-%d %H:%M:%S'))
                    else:
                        time2 = time.mktime(time.strptime(time1_str1.strip(), '%Y-%m-%d %H:%M'))
                else:
                    time2 = time.mktime(time.strptime(time1_str1.strip(), '%Y-%m-%d'))
                assert int(time1) >= int(time2), '排序错误'
            else:
                assert 1 == 1
        except AssertionError as e:
            error_log().debug(e)
            raise e

    def make_assert_by_text_different(self, args: str, assert_data: str, context=None):
        '''
        当文本信息不相等时的断言
        :param args: 断言的元素1
        :param assert_data: 断言的值
        :param context:
        :return:
        '''
        try:
            text = self.kw.get_text(args=args, context=None)
            assert assert_data not in text, '断言失败，获取的文本信息包含断言信息'
        except Exception as e:
            # 判断是否暂无数据,是，用例执行通过
            try:
                args = ('xpath', '//p[contains(text(), "暂无数据")]')
                self.kw.locator(args=args, context="暂无数据")
            except Exception as E:
                error_log().debug(e)
                raise e

    def make_assert_by_text_is_null(self, args: str, context=None):
        '''
        判断值是否为空值
        :param args: 断言的元素
        :param assert_data:
        :param context:
        :return:
        '''
        try:
            text = self.kw.get_text(args=args, context=None)
            assert text is not None, '断言失败，返回的数据为空'
        except AssertionError as e:
            error_log().debug(e)
            raise e

    def make_assert_by_elements_text(self, args: str, assert_data: str, context=None):
        '''
        根据当前页面多个元素的text()进行断言
        :param args: 断言的元素
        :param assert_data: 断言的值
        :param context:
        :return:
        '''
        try:
            for element in self.kw.locator_elements(args=args, context=None):
                assert assert_data in element.text, '断言失败，当前页面页面存在筛选条件外的元素'
        except Exception as e:
            # 判断是否暂无数据,是，用例执行通过
            try:
                args = ('xpath', '//p[contains(text(), "暂无数据")]')
                self.kw.locator(args=args, context="暂无数据")
            except Exception as E:
                error_log().debug(e)
                raise e

    def make_assert_by_value_in_elements_text_list(self, args: str, assert_data: str, context=None):
        '''
        根据当前页面多个元素的text()进行断言
        :param args: 断言的元素
        :param assert_data: 断言的值
        :param context:
        :return:
        '''
        try:
            elements_texts = []
            for element in self.kw.locator_elements(args=args, context=None):
                elements_texts.append(element.text)
            assert assert_data in elements_texts, '断言失败，当前页面不包含需要查找的元素'
        except Exception as e:
            # 判断是否暂无数据,是，用例执行通过
            try:
                args = ('xpath', '//p[contains(text(), "暂无数据")]')
                self.kw.locator(args=args, context="暂无数据")
            except Exception as E:
                error_log().debug(e)
                raise e

    def make_assert_by_elements_no_exist(self, args: str, assert_data: str, context=None):
        '''
        根据元素是否存在进行断言
        :param args: 断言的元素
        :param assert_data: 断言 的值
        :param context:
        :return:
        '''
        try:
            # 如果元素存在，则说明操作失败
            self.kw.locator(args=args, context=None)
            assert 1 == 2, '断言失败, 当前元素在页面当中存在'
        except Exception as e:
            assert 1 == 1, '断言成功, 页面数据不存在'

    def make_assert_by_elements_more_values(self, args: str, assert_data: list, context=None):
        '''
        根据当前页面多个元素的text()进行断言,判断出入的多个断言值是否在获得的文本数据当中
        :param args: 断言的元素
        :param assert_data: 断言的值
        :param context:
        :return:
        '''
        error_value = None
        try:
            for element in self.kw.locator_elements(args=args, context=None):
                error_value = element.text
                assert element.text.strip() in assert_data, '断言失败，获取到的文本包含其他值'
        except Exception as e:
            # 判断是否暂无数据,是，用例执行通过
            try:
                args = ('xpath', '//p[contains(text(), "暂无数据")]')
                self.kw.locator(args=args, context="暂无数据")
            except Exception as E:
                error_log().debug(e)
                error_log().debug(f'========>>错误的值是{error_value}')
                raise e
